#include <iostream>
#include <unistd.h>

using namespace std;

int main()
{
	cout<<"父进程启动"<<endl;
	pid_t id = fork();
	if (id<0) //-1
	{
		perror("fork fail");
		return 1;
	}
	else if (id == 0)
	{
		int i = 6;
		cout<<"子进程启动"<<endl;
		sleep(5);
		while (i--)
		{
			cout<<i<<"子进程执行中,pid为"<<getpid()<<"其父进程ppid为"<<getppid()<<endl;
			sleep(2);
		}
	}
	else
	{
		int j = 3;
		while(j--)
		{
			cout<<j<<"父进程执行中,pid为"<<getpid()<<"其父进程ppid为"<<getppid()<<endl;
			sleep(2);
		}
	}
	cout<<"程序结束运行"<<endl;
	return 0;
}

